﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace FilesTubeClient
{
    public class Page
    {
        private XmlDocument _xml_doc = null;
        //--------------------------------------------------------------------------------
        private int _totalResults = 0;
        public int TotalResults
        {
            get { return _totalResults; }
        }
        //--------------------------------------------------------------------------------
        private int _resultsInPage = 0;
        public int ResultsInPage
        {
            get { return _resultsInPage; }
        }
        //--------------------------------------------------------------------------------
        private int _pageNumber = 0;
        public int PageNumber
        {
            get { return _pageNumber; }
        }
        //--------------------------------------------------------------------------------
        private List<ResultItem> _results = new List<ResultItem>();
        public ResultItem[] Results
        {
            get { return _results.ToArray(); }
        }
        //--------------------------------------------------------------------------------
        public Page(string pageXml)
        {
            _xml_doc = new XmlDocument();
            _xml_doc.LoadXml(pageXml);
            parse();
        }
        //--------------------------------------------------------------------------------
        private void parse()
        {
 	        // check if results have been found
            if(int.Parse(_xml_doc.SelectSingleNode("/answer/hasResults").InnerText) == 0)
                return;
            
            // set props
            _totalResults = int.Parse(_xml_doc.SelectSingleNode("/answer/results/hitsTotal").InnerText);
            _pageNumber = int.Parse(_xml_doc.SelectSingleNode("/answer/results/pageNumber").InnerText);
            _resultsInPage = int.Parse(_xml_doc.SelectSingleNode("/answer/results/hitsForThisPage").InnerText);

            // set results
            XmlNodeList nodes = _xml_doc.SelectNodes("//hits");
            foreach(XmlNode node in nodes)
                _results.Add(new ResultItem(node));
        }
        //--------------------------------------------------------------------------------
    }
}
